package com.tanxiuxin.YiWaiMai.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanxiuxin.YiWaiMai.common.Result;
import com.tanxiuxin.YiWaiMai.dto.SetmealDto;
import com.tanxiuxin.YiWaiMai.pojo.Category;
import com.tanxiuxin.YiWaiMai.pojo.Setmeal;
import com.tanxiuxin.YiWaiMai.service.CateGoryService;
import com.tanxiuxin.YiWaiMai.service.DishService;
import com.tanxiuxin.YiWaiMai.service.SetmealDishService;
import com.tanxiuxin.YiWaiMai.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.stream.Collectors;

/**
 *    套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CateGoryService cateGoryService;

    @Autowired
    private DishService dishService;

    /**
     *       新增套餐
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache" , allEntries = true) // 表示删除 setmealCache 下所有缓存数据
    @PostMapping
    public Result<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);

        return Result.success("新增套餐成功");
    }

    /**
     *      套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page , int pageSize , String name) {
        // 构造分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();
        // 构造条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(!StringUtils.isEmpty(name), Setmeal::getName, name);
        // 添加排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        // 执行查询
        setmealService.page(pageInfo, queryWrapper);
        // 对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage , "records");

        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item)-> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item , setmealDto);
            Long categoryId = item.getCategoryId(); // 分类的 id
            Category category = cateGoryService.getById(categoryId);// 根据 id 查分类对象
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        return Result.success(dtoPage);
    }

    /**
     *       根据 id 来查询套餐信息和对应的信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<SetmealDto> get(@PathVariable("id") Long id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return Result.success(setmealDto);
    }

    /**
     *      修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache" , allEntries = true) // 表示删除 setmealCache 下所有缓存数据
    public Result<String> update(@RequestBody SetmealDto setmealDto){

        setmealService.updateWithDish(setmealDto);
        return Result.success("修改套餐成功");
    }


    /**
     *      删除套餐
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmealCache" , allEntries = true) // 表示删除 setmealCache 下所有缓存数据
    @DeleteMapping
    public Result<String> delete(Long... ids){
        setmealService.removeWithDish(ids);
        return Result.success("删除成功");
    }

    /**
     *      根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmealCache" , key = "#setmeal.categoryId + '_' + #setmeal.status")
    @GetMapping("/list")
    public Result<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null , Setmeal::getCategoryId , setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null , Setmeal::getStatus , setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return Result.success(list);
    }

    /**
     *    起售和停售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache" , allEntries = true)
    public Result<String> stop( @PathVariable("status")Integer status , Long... ids){
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Setmeal::getId , ids);
        List<Setmeal> setmealList = setmealService.list(lambdaQueryWrapper);
        for (Setmeal setmeal : setmealList){
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return Result.success("套餐状态更改成功");
    }
}
